/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.dao;

import com.mycompany.delivery.entities.Contact;
import com.mycompany.delivery.entities.Delivery;
import com.mycompany.delivery.entities.State;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Martin Horvath
 */
@Repository(value = "deliveryDao")
public class DeliveryDAOImpl implements DeliveryDAO {

    @PersistenceContext(unitName = "TestPU")
    private EntityManager manager;

    @Override
    public void createDelivery(Delivery delivery) {
        try {
            manager.persist(delivery);
        } catch (IllegalArgumentException | NullPointerException e) {
            throw new DataAccessException("Problem with argument" + e.getMessage(), e) {
            };

        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage(), ex) {
            };
        }
    }

    @Override
    public void updateDelivery(Delivery delivery) {
        try {
            manager.merge(delivery);
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }

    @Override
    public void deleteDelivery(Delivery delivery) {
        try {
            manager.remove(manager.find(Delivery.class, delivery.getId()));
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }

    @Override
    public Delivery getDeliveryById(Long id) {
        Delivery delivery;
        try {
            delivery = (Delivery) manager.find(Delivery.class, id);
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return (Delivery) delivery;
    }

    @Override
    public List<Delivery> getAllDeliveries() {
        List<Delivery> deliveries;
        try {
            deliveries = manager.createQuery("SELECT s FROM Delivery s").getResultList();
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return deliveries;
    }

    @Override
    public List<Delivery> getDeliveriesBySendingDate(Date date) {
        List<Delivery> deliveries;
        try {
            deliveries = manager.createQuery("SELECT d FROM Delivery d WHERE d.sendingDate = :sd").setParameter("sd", date).getResultList();
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return deliveries;
    }

    @Override
    public List<Delivery> getDeliveriesByReceivingDate(Date date) {
        List<Delivery> deliveries;
        try {
            deliveries = manager.createQuery("SELECT d FROM Delivery d WHERE d.deliveryDate = :rd").setParameter("rd", date).getResultList();
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return deliveries;
    }

    @Override
    public List<Delivery> getDeliveriesByState(State state) {
        List<Delivery> deliveries;
        try {
            deliveries = manager.createQuery("SELECT d FROM Delivery d WHERE d.state = :st").setParameter("st", state).getResultList();
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return deliveries;
    }

    @Override
    public List<Delivery> getDeliveriesBySender(Contact sender) {
        List<Delivery> deliveries;
        try {
            deliveries = manager.createQuery("SELECT d FROM Delivery d WHERE d.sender = :st").setParameter("st", manager.find(Contact.class, sender.getId())).getResultList();//manager.createQuery("SELECT d.DEL_ID FROM DEL_REC d WHERE DEL_ID LIKE " +  sender.getId()).getResultList();
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return deliveries;
    }

    @Override
    public List<Delivery> getDeliveriesByReceiver(Contact receiver) {
        List<Delivery> deliveries;
        try {
            deliveries = manager.createQuery("SELECT d FROM Delivery d WHERE d.receiver = :st").setParameter("st", manager.find(Contact.class, receiver.getId())).getResultList();//manager.createQuery("SELECT d FROM Delivery d WHERE d.id IN (SELECT c.id FROM DEL_REC c WHERE c.REC_ID LIKE " + receiver.getId() + ")" /*manager.find(Contact.class, receiver.getId())*/).getResultList();
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return deliveries;
    }

}
